﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCEdu;
using System.Data;

public partial class ContractRegister : System.Web.UI.Page
{
    string fileURL = "";
    Customers objCus = new Customers();
    TutorProfile objTutor = new TutorProfile();
    ContractHistory objCont = new ContractHistory();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack) 
        {
            try
            {
                lblFileName.Visible = false;
                btnChange.Visible = false;
                lblContNameValid.Visible = false;
                lblFileValid.Visible = false;
                chkCusAdd.Visible = false;
                int id = int.Parse(Request.QueryString["id"].ToString());
                string type = Request.QueryString["type"].ToString();
                if (type.Equals("Tutor"))
                    {
                        loadCustomer();
                        loadTutorByID(id);
                    }
                    else
                    {
                        if (type.Equals("Cus"))
                        {
                            loadTutor();
                            loadCustomerByID(id);
                        }
                    }                
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "ex", "<script>alert('"+ ex.ToString() +"')</script>");   
            }
        }
    }

    private void loadCustomer()
    {
        DataSet dsCus = new DataSet();
        dsCus = objCus.GetAll();
        cmbCusName.DataSource = dsCus;
        cmbCusName.DataTextField = "Name";
        cmbCusName.DataValueField = "ID";
        cmbCusName.DataBind();
        cmbTutorName.Visible = false; 
        lblContName.Text = "Chọn khách hàng"; 
    }

    private void loadCustomerByID(int Cusid)
    {
        DataSet dsCust = new DataSet();
        dsCust = objCus.Get(Cusid);
        lblCusName.Text = dsCust.Tables[0].Rows[0]["Name"].ToString();
        lblCusID.Text = objCus.GenID("000000",Cusid.ToString());
        lblCusAdd.Text = dsCust.Tables[0].Rows[0]["Address"].ToString();
        lblCusPhone.Text = dsCust.Tables[0].Rows[0]["Telephone"].ToString();        
    }

    private void loadTutor()
    {
        DataSet dsTut = new DataSet();
        dsTut = objTutor.GetAll();
        cmbTutorName.DataSource = dsTut;
        cmbTutorName.DataTextField = "Name";
        cmbTutorName.DataValueField = "ID";
        cmbTutorName.DataBind();
        cmbCusName.Visible = false;
        lblContName.Text = "Chọn gia sư"; 
    }

    private void loadTutorByID(int Tutorid)
    {
        DataSet dsTutor = new DataSet();
        dsTutor = objTutor.Get(Tutorid);
        lblTutorName.Text = dsTutor.Tables[0].Rows[0]["Name"].ToString();
        lblTutorID.Text = dsTutor.Tables[0].Rows[0]["IDNumber"].ToString();
        lblTutorAdd.Text = dsTutor.Tables[0].Rows[0]["Address"].ToString();
        lblTutorPhone.Text = dsTutor.Tables[0].Rows[0]["Phone"].ToString();               
    }

    private void DelFile()
    {
        fileURL = Server.MapPath("/ContractFiles/" + FileUpload1.FileName);
        System.IO.File.Delete(fileURL);
        lblFileName.Text = "";
    }

    protected void cmbCusName_SelectedIndexChanged(object sender, Obout.ComboBox.ComboBoxItemEventArgs e)
    {
        try
        {
            int Cid = int.Parse(this.cmbCusName.SelectedValue.ToString());
            loadCustomerByID(Cid);
            chkCusAdd.Visible = true;
        }
        catch (Exception ex)
        { 
        }
    }
    protected void cmbTutorName_SelectedIndexChanged(object sender, Obout.ComboBox.ComboBoxItemEventArgs e)
    {
        int Tid = int.Parse(this.cmbTutorName.SelectedValue.ToString());
        loadTutorByID(Tid);
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (this.FileUpload1.FileName.Equals(""))
        {
            lblFileValid.Visible = true;
            this.lblFileValid.Text = "Chưa chọn file";
        }
        else
        {
            fileURL = Server.MapPath("/ContractFiles/" + FileUpload1.FileName);
            lblFileName.Text = FileUpload1.FileName.ToString();
            FileUpload1.SaveAs(fileURL);
            lblFileName.Visible = true;
            btnUpload.Visible = false;
            FileUpload1.Visible = false;
            btnChange.Visible = true;
            this.lblFileValid.Text = "";
            txtCurMark.Focus();
        }
    }
    protected void btnChange_Click(object sender, EventArgs e)
    {
        btnUpload.Visible = true;
        FileUpload1.Visible = true;
        btnChange.Visible = false;
        this.lblFileName.Visible = false;
        DelFile();
        FileUpload1.Focus();
    }
    protected void Reset_Click(object sender, EventArgs e)
    {
        this.txtContPlace.Text = "";
        this.txtContClass.Text = "";
        this.txtContDate.Text = "";
        this.txtContDay.Text = "";
        this.txtContEndHour.Text = "";
        this.txtContNumber.Text = "";
        this.txtContPrice.Text = "";
        this.txtContPupil.Text = "";
        this.txtContStartHour.Text = "";
        this.txtContSubj.Text = "";
        this.txtCurMark.Text = "";
        this.txtExpMark.Text = "";
        this.txtFirstPaid.Text = "";
        this.txtStartDate.Text = "";
        this.drContStatus.SelectedIndex = 0;
        this.drIsPaid.SelectedIndex = 0;
        txtContNumber.Focus();
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        int TutorID = int.Parse(this.lblTutorID.Text);
        int CusID = int.Parse(this.lblCusID.Text);
        string ContNumber = txtContNumber.Text.ToString();
        DateTime ContDate = DateTime.Parse(txtContDay.Text.ToString());
        string place = txtContPlace.Text.ToString();
        string subject = txtContSubj.Text.ToString();
        string _class = txtContClass.Text.ToString();
        int pupil = int.Parse(txtContPupil.Text.ToString());
        string dayPerWeek = txtContDate.Text.ToString();
        string startHour = txtContStartHour.Text.ToString();
        string endHour = txtContEndHour.Text.ToString();
        double price = double.Parse(txtContPrice.Text.Trim());
        double firstPaid = double.Parse(txtFirstPaid.Text.Trim());
        double secondPaid = 0.0;
        bool paid = bool.Parse(drIsPaid.SelectedValue.ToString());
        DateTime startDate = DateTime.Parse(txtStartDate.Text.ToString());
        fileURL = Server.MapPath("/ContractFiles/" + lblFileName.Text);
        string curMark = txtCurMark.Text.ToString();
        string expMark = txtExpMark.Text.ToString();
        string status = drContStatus.SelectedValue.ToString();
        if (cmbCusName.SelectedValue.Equals("") || cmbTutorName.SelectedValue.Equals(""))
        {
            lblFileValid.Visible = true;
            lblContNameValid.Text = "Chưa chọn khách hàng hoặc gia sư";
        }
        else
        {
            if (objCont.Add(TutorID, CusID, ContNumber, ContDate, fileURL, place, subject, _class, pupil, startHour, endHour, dayPerWeek, price, firstPaid, secondPaid, startDate, paid, curMark, expMark, "", status) != -1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "ex", "<script>alert('Nhập liệu thành công')</script>");
                Response.Redirect("CustomerRegister.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "ex", "<script>alert('Lỗi nhập liệu')</script>");
            }
        }
    }
    protected void chkCusAdd_CheckedChanged(object sender, EventArgs e)
    {
        if (chkCusAdd.Checked == true)
        {
            txtContPlace.Text = lblCusAdd.Text.ToString();
            txtContPlace.Enabled = false;
        }
        else
        {
            txtContPlace.Text = "";
            txtContPlace.Enabled = true;
        }
    }    
}